package com.sservice.banfu.controller;

import com.sservice.banfu.e.AuctionRegister;
import com.sservice.banfu.e.SalePerson;
import com.sservice.banfu.service.AuctionRegisterService;
import com.sservice.commonBusiness.service.PLetterService;
import com.sservice.banfu.service.SalePersonService1;
import com.sservice.banfu.vo.SalePersonVo;
import com.sservice.base.entity.dto.BaseResult;
import com.sservice.base.entity.dto.ListResult;
import com.sservice.base.entity.dto.Msg;
import com.sservice.base.entity.dto.Pagination;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by byte_su@163.com on 2018/01/25.
 */
@Controller
@RequestMapping("/admin/auctionRegister/")
public class AuctionRegisterController {

    @Autowired
    AuctionRegisterService auctionRegisterService;
    @Autowired
    PLetterService pLetterService;
    @Autowired
    SalePersonService1 salePersonService1;

    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult add(AuctionRegister auctionRegister) {
        return new BaseResult(true, String.valueOf(auctionRegisterService.save(auctionRegister)), 200);
    }

    @RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult delete( AuctionRegister auctionRegister) {
        return new BaseResult(true, String.valueOf(auctionRegisterService.deleteById(auctionRegister.getId())), 200);
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult update(AuctionRegister auctionRegister) {
        return new BaseResult(true, String.valueOf(auctionRegisterService.update(auctionRegister)), 200);
    }


    /**
     * 竞拍报名审核
     *
     * @param auctionRegister
     * @return
     */
    @RequestMapping(value = "check", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult check(AuctionRegister auctionRegister, HttpSession session) {

        AuctionRegister ar = auctionRegisterService.selectById(auctionRegister.getId());
        if (ar == null) {
            return new BaseResult(false, "非法操作", 403);
        }
        String msg = "";
        if ((Integer.valueOf(1)).equals(auctionRegister.getStatus())) { // 审核通过
            SalePerson salePerson = new SalePerson();
            salePerson.setLevel("2");
            salePerson.setId(Integer.valueOf(ar.getOwnerId()));
            salePersonService1.update(salePerson); // 更新
            msg = "您提交的竞拍报名审核通过！";
            pLetterService.toMsg(msg, "sys", ar.getOwnerId(), "[系统消息]");
        } else if ((Integer.valueOf(4)).equals(auctionRegister.getStatus())) {
            msg = "您提交的竞拍报名被拒绝：" + auctionRegister.getReason();
            pLetterService.toMsg(msg,
                    "sys", ar.getOwnerId(), "[系统消息]");
        }
        // 发短信
        SalePersonVo salePerson1 = salePersonService1.selectByPrimaryKey(Integer.valueOf(ar.getOwnerId()));
        if (salePerson1.getPhone() != null) {
            Msg msg1 = new Msg();
            msg1.setPhone(salePerson1.getPhone());
            msg1.setMsg(msg);
            session.setAttribute(Msg.KEY, msg1);
        }

        //JavaSmsApi.sender(capText, phone);
        return new BaseResult(true, String.valueOf(auctionRegisterService.update(auctionRegister)), 200);
    }

    @RequestMapping("detail/{id}")
    @ResponseBody
    public BaseResult detail(@RequestParam Integer id) {
        AuctionRegister auctionRegister = auctionRegisterService.findById(id);
        return new BaseResult(true, auctionRegister);
    }

    @RequestMapping("list")
    @ResponseBody
    public ListResult list(AuctionRegister ar) {
        List<AuctionRegister> list = auctionRegisterService.query(ar);
        int count = auctionRegisterService.count(ar);
        Pagination pagination = new Pagination(ar.getPage(), count, ar.getSize());
        ListResult lr = new ListResult(true, list);
        lr.setPage(pagination);
        return lr;
    }
}
